package fun.boomcat.luckyhe.mirai.plugin.dbutils;

import fun.boomcat.luckyhe.mirai.plugin.mapper.ServeGroupMapper;
import fun.boomcat.luckyhe.mirai.plugin.utils.MyBatisReplyTableUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.util.List;

public class ServeGroupListModifier {
    private static List<Long> groupIds = null;

    private static List<Long> getGroupIds() throws IOException {
        if (groupIds == null) {
            groupIds = ServeGroupListModifier.selectAllGroupId();
        }
        return groupIds;
    }

    public static int createServeGroupTable() throws IOException {
        SqlSession sqlSession = MyBatisReplyTableUtil.getSqlSession();
        int res = sqlSession.getMapper(ServeGroupMapper.class).createServeGroupTable();
        sqlSession.commit();
        sqlSession.close();

        return res;
    }

    public static int deleteGroupId(long groupId) throws IOException {
        SqlSession sqlSession = MyBatisReplyTableUtil.getSqlSession();
        int res = sqlSession.getMapper(ServeGroupMapper.class).deleteGroupId(groupId);
        sqlSession.commit();
        sqlSession.close();
        groupIds = ServeGroupListModifier.selectAllGroupId();

        return res;
    }

    public static int insertGroupId(long groupId) throws IOException {
        SqlSession sqlSession = MyBatisReplyTableUtil.getSqlSession();
        int res = sqlSession.getMapper(ServeGroupMapper.class).insertGroupId(groupId);
        sqlSession.commit();
        sqlSession.close();
        groupIds = ServeGroupListModifier.selectAllGroupId();

        return res;
    }

    public static List<Long> selectAllGroupId() throws IOException {
        SqlSession sqlSession = MyBatisReplyTableUtil.getSqlSession();
        List<Long> res = sqlSession.getMapper(ServeGroupMapper.class).selectAllGroupId();
        sqlSession.close();

        return res;
    }

    public static boolean checkGroup(long groupId) throws IOException {
        List<Long> groupIds = getGroupIds();
        return groupIds.contains(groupId);
    }

}
